using imitateMacQQ.WebApi.Configures;
using imitateMacQQ.WebApi.Domain.CircleServe.Entity.dao;
using imitateMacQQ.WebApi.Domain.CircleServe.Interface;
using imitateMacQQ.WebApi.Domain.Request.utils;
using imitateMacQQ.WebApi.Repository.EFCore.Model;
using imitateMacQQ.WebApi.Repository.EFCore.Model.Circle;
using Microsoft.EntityFrameworkCore;

namespace imitateMacQQ.WebApi.Domain.CircleServe.Service;

public class GetCircleService : IGetCircle
{
    private readonly INLogHelper _logHelper;

    public GetCircleService(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }

    public async Task<Request<DimCircleData[]>> GetDimDataCircle(string account)
    {
        try
        {
            await using var db = new DataDbContext();
            var resDataCircles = new List<FriendCircle>();
            var info = db.Buddies.Where(e =>
                    e.IsUsing == 1 && (e.RequestAccount == account
                                       || e.ResultAccount == account))
                .ToArray();

            var data0 = db.FriendCircles
                .Where(e =>
                    e.SendAccount == account
                )
                .ToArray();
            resDataCircles.AddRange(data0);

            foreach (var item in info)
            {
                var data = db.FriendCircles
                    .Where(e =>
                        (e.IsShow && (e.SendAccount == item.RequestAccount
                                      || e.SendAccount == item.ResultAccount)
                                  && e.SendAccount != account)
                    )
                    .ToArray();
                resDataCircles.AddRange(data);
            }


            var resData = new DimCircleData[resDataCircles.Count()];
            var index = 0;
            foreach (var item in
                     resDataCircles.OrderByDescending(e => e.SendTime).ToArray())
            {
                resData[index] = new DimCircleData()
                {
                    SendTime = item.SendTime,
                    SendAccount = item.SendAccount,
                    Content = item.Content,
                    LikeSum = item.LikeSum,
                    Remarks = item.Remarks
                };
                index++;
            }

            return Request<DimCircleData[]>.Ok(resData);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<DimCircleData[]>.Ok(null!, false, ex.Message);
        }
    }

    public async Task<Request<CircleAllData>> GetAllDataCircle(string account, string remark)
    {
        try
        {
            await using DataDbContext db = new DataDbContext();
            var info = await db.FriendCircles.FirstOrDefaultAsync(e => e.Remarks == remark);
            if (info is null)
                throw new Exception("Data Is Null! Already Del");
            var res = new CircleAllData()
            {
                SendTime = info.SendTime,
                SendAccount = info.SendAccount,
                Content = info.Content,
                LikeSum = info.LikeSum,
                HasPrintPicture = info.HasPrintPicture,
                PrintPicture = info.PrintPicture,
                Remarks = info.Remarks
            };
            return Request<CircleAllData>.Ok(res);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<CircleAllData>.Ok(null!, false, ex.Message);
        }
    }

    public async Task<Request<string>> GetName(string account)
    {
        try
        {
            await using DataDbContext db = new DataDbContext();
            var res = await db.Users.FirstOrDefaultAsync(e => e.Account == account);
            if (res is null)
                throw new Exception("USER IS NULL");
            return Request<string>.Ok(res.UserName ?? " ");
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<string>.Ok(null!, false, ex.Message);
        }
    }

    public async Task<Request<OtherData>> GetOther(string account, string send, string remark)
    {
        try
        {
            var res = new OtherData();
            await using DataDbContext db = new DataDbContext();
            var infoUsr = await db.Users.FirstOrDefaultAsync(e => e.Account == send);
            if (infoUsr is null)
                throw new Exception("User is DEl");
            res.IsVip = infoUsr.IsVip;
            var pos = await db.Likes.FirstOrDefaultAsync(e =>
                e.Remarks == remark && e.Account == account);
            if (pos is not null)
                res.IsLike = true;

            return Request<OtherData>.Ok(res);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<OtherData>.Ok(null!, false, ex.Message);
        }
    }
}